package com.bawei.module_inspection.fl

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_inspection.R
import com.bawei.module_inspection.databinding.FragmentInspectionRecordBinding
import com.bawei.module_inspection.databinding.FragmentWorkOrderRecordBinding
import com.bawei.module_inspection.fl.ui.InspectionRecordViewModel
import com.bawei.module_inspection.fl.ui.InspectionWordViewModel
import kotlinx.coroutines.launch


class WorkOrderRecordFragment : Fragment() {

    private lateinit var binding: FragmentWorkOrderRecordBinding
    private val viewModel: InspectionWordViewModel by viewModels() // 需创建对应的 ViewModel
    private val recordAdapter by lazy { WordListAdapter() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentWorkOrderRecordBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.rvWord.apply {
            adapter = recordAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }

        lifecycleScope.launch {
            viewModel.recordList.collect { data ->
                if (data.isNotEmpty()) {
                    recordAdapter.submitList(data)
                    binding.rvWord.visibility = View.VISIBLE
                    binding.ivEmpty.visibility = View.GONE
                } else {
                    binding.rvWord.visibility = View.GONE
                    binding.ivEmpty.visibility = View.VISIBLE
                }
            }
        }
    }
}
class WordListAdapter : ListAdapter<String, WordViewHolder>(DiffUtilCallbackWord()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_standard, parent, false)
        return WordViewHolder(view)
    }

    override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
}

class WordViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    fun bind(item: String) {
        itemView.apply {
            itemView.findViewById<TextView>(R.id.tv_standard_text).text = item
        }
    }
}

class DiffUtilCallbackWord : DiffUtil.ItemCallback<String>() {
    override fun areItemsTheSame(oldItem: String, newItem: String) = oldItem == newItem
    override fun areContentsTheSame(oldItem: String, newItem: String) = oldItem == newItem
}